capture program drop rdrobust_export
program define rdrobust_export, eclass

	syntax anything [if] [in] [, fuzzy(string) h(string) kernel(string) file(string) ///
		apprep(string) vce(string) c(string) covs(string) p(string) colstring(string) bwselect(string) ///
		reguterm(string)]

	preserve
		tokenize "`anything'"
		local dep `1'
		local indep `2'

		*Global that will extract results from rdrobust
		global results "N_h_l N_h_r h_l h_r tau_cl tau_bc se_tau_cl se_tau_rb"

		*-----------------------*
		* Producing the Results *
		*-----------------------*
		* Type of kernel
		if "`kernel'"=="" {
			local ker=""
		}
		else if "`kernel'"!="" {
			local ker="kernel(`kernel')"
		}

		* h bandwidth
		if "`h'"=="" {
			local h_band=""
		}
		else if "`h'"!="" {
			local h_band="h(`h' `h')"
		}

		* Type of VCE
		if "`vce'"=="" {
			local vce_type=""
		}
		else if "`vce'"!="" {
			local vce_type="vce(`vce')"
		}

		* c cutoff
		if "`c'"=="" {
			local cutoff=""
		}
		else if "`c'"!="" {
			local cutoff="c(`c')"
		}

		* Covariates
		if "`covs'"=="" {
			local covariates=""
		}
		else if "`covs'"!="" {
			local covariates="covs(`covs')"
		}

		* Polynomial order
		if "`p'"=="" {
			local poly=""
		}
		else if "`p'"!="" {
			local poly="p(`p')"
		}

		* Fuzzy
		if "`fuzzy'"=="" {
			local fuz=""
		}
		else if "`fuzzy'"!="" {
			local fuz="fuzzy(`fuzzy')"
		}

		* Bwselect
		if "`bwselect'"=="" {
			local bw=""
		}
		else if "`bwselect'"!="" {
			local bw="bwselect(`bwselect')"
		}

		* Regularization term
		if "`reguterm'"=="" {
			local rt=""
		}
		else if "`reguterm'"!="" {
			local rt="scaleregul(0)"
		}

		*Estimating
		rdrobust `dep' `indep' `if', ///
			all `fuz' `ker' `h_band' `vce_type' `cutoff' `covariates' `poly' `bw' `rt'

		*Extracting results
		foreach x of global results {
			local `x'_res=e(`x')
		}

		*-------------------------*
		* Constructing the Tables *
		*-------------------------*
		clear
		set obs 1

		* Filling the table
		foreach b in estim band_l band_r conv_pv conv_ci_low conv_ci_high rob_pv rob_ci_low rob_ci_high obs_l obs_r obs {
			gen `b'=.
		}

		replace estim=`tau_cl_res'

		replace band_l=`h_l_res'
		replace band_r=`h_r_res'

		replace conv_pv=2*normal(-abs(`tau_cl_res'/`se_tau_cl_res'))
		replace conv_ci_low=`tau_cl_res'-invnormal(0.975)*`se_tau_cl_res'
		replace conv_ci_high=`tau_cl_res'+invnormal(0.975)*`se_tau_cl_res'

		replace rob_pv=2*normal(-abs(`tau_bc_res'/`se_tau_rb_res'))
		replace rob_ci_low=`tau_bc_res'-invnormal(0.975)*`se_tau_rb_res'
		replace rob_ci_high=`tau_bc_res'+invnormal(0.975)*`se_tau_rb_res'

		replace obs_l=`N_h_l_res'
		replace obs_r=`N_h_r_res'
		replace obs=`N_h_l_res'+`N_h_r_res'

		if "`colstring'"!="" {
			gen colstring="`colstring'"
		}

		save "tempfile.dta", replace
		
		*--------------------------------*
		* Appending or replacing to save *
		*--------------------------------*
		if "`apprep'"=="replace" {
			erase "tempfile.dta"
		}
		else if "`apprep'"=="append" {
			use "`file'.dta", clear

			append using "tempfile.dta"
			erase "tempfile.dta"
		}

		save "`file'.dta", replace
	restore
end
